<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
    <style>
        * {
            margin: 0;
        }

        #box {
            height: 100px;
            width: 100px;
            background: red;
            position: absolute;
            cursor: move;
            background-size: 100px 100px;
        }
    </style>
</head>

<body>
    <div id="box"></div>
    <script>
        var box = document.getElementById('box')
        move(box)

        function move(id) {
            id.onmousedown = function (evt) {
                var e = evt || event
                //获得鼠标位置相对当前元素的偏移量
                var ofX = e.offsetX
                var ofY = e.offsetY
                document.onmousemove = function (evt) {
                    var e = evt || event
                    //获得当前元素的top和left值
                    var x = e.pageX - ofX
                    var y = e.pageY - ofY
                    if (x < 0) {
                        x = 0
                    }
                    var maxX = window.innerWidth - id.offsetWidth
                    if (x > maxX) {
                        x = maxX
                    }
                    if (y < 0) {
                        y = 0
                    }
                    var maxY = window.innerHeight - id.offsetHeight
                    if (y > maxY) {
                        y = maxY
                    }
                    id.style.left = x + 'px'
                    id.style.top = y + 'px'
                }
                document.onmouseup = function () {
                    document.onmousemove = ''
                }
            }
        }
    </script>
</body>

</html>